using Microsoft.Phone.Tasks;
using System.Windows.Input;
using System;
using TaxiPlease.PhoneHelpers;

namespace TaxiPlease.ViewModels
{
	public class SendMessageCommand : ICommand
	{
		private readonly MainViewModel viewModel;

		public SendMessageCommand(MainViewModel viewModel)
		{
			this.viewModel = viewModel;
			this.viewModel.PropertyChanged += (s, e) =>
				{
					if (e.PropertyName == "Address")
					{
						if (CanExecuteChanged != null)
						{
							CanExecuteChanged(this, EventArgs.Empty);
						}
					}
				};
		}

		public bool CanExecute(object parameter)
		{
			return this.viewModel != null && this.viewModel.Address != MainViewModel.AddressDefault && this.viewModel.Address != MainViewModel.LocationDisabled;
		}

		public event EventHandler CanExecuteChanged;

		public void Execute(object parameter)
		{
			ShowSms(this.viewModel.Address);
		}

		public void ShowSms(string address)
		{
			var sms = viewModel.Options.SmsPattern.Replace("<address>", address);
			SmsComposeTask composeSMS = new SmsComposeTask
			{
				Body = sms,
				To = viewModel.Options.TaxiPhoneNumber
			};
			composeSMS.Show();
		}
	}
}